package com.wc.算法提高课.A第一章_动态规划.单调队列优化DP.修剪草坪;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/13 23:45
 * @description https://www.acwing.com/problem/content/1089/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010;
    static long INF = Long.MAX_VALUE;
    // f[i] 表示 i 不选的最小值, 保证不超过K头不选, 相当于K里面至少选一个表示不选
    static long[] f = new long[N];
    static int[] q = new int[N];
    static int[] w = new int[N];
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        long sum = 0;
        for (int i = 1; i <= n; i++) {
            w[i] = sc.nextInt();
            sum += w[i];
        }
        int hh = 1, tt = 1;
        for (int i = 1; i <= n; i++) {
            while (i - m - 1 > q[hh]) hh++;
            f[i] = w[i] + f[q[hh]];
            while (hh <= tt && f[i] <= f[q[tt]]) tt--;
            q[++tt] = i;
        }
        long res = INF;
        for (int i = n - m; i <= n; i++) res = Math.min(res, f[i]);
        res = sum - res;
        out.println(res);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
